package com.itheima.excption;

import com.itheima.vo.ErrorResult;
import com.itheima.vo.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//结束controller所有的异常
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {
    //处理BusinessException
    @ExceptionHandler(BusinessException.class)
    public Result business(BusinessException businessException) {
        //1.打印异常信息
        businessException.printStackTrace();
        //2.给出响应
        ErrorResult errorResult = businessException.getErrorResult();
        //响应码500,响应体
        return Result.error(null, "errorResult");
    }

    @ExceptionHandler(MyException.class)
    public ResponseEntity<Result> myException(MyException myException) {
        //1.打印异常信息
        log.error(myException.getMessage());

        return ResponseEntity.ok(Result.errorMy(myException.getMessage()));
    }

    //处理Exception
    @ExceptionHandler(Exception.class)
    public ResponseEntity exception(Exception exception) {
        exception.printStackTrace();
        return ResponseEntity.ok(Result.errorSys());
    }
}
